package com.example.demo.saturn;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ProductSaturn {

	public static String prefixUrl = "https://saturn.wuuxiang.com";
	public static String userName = "admin";
	public static String userPwd = "13752168201";

	public static final String jobGroup = "8";

	public static String readerJdbcUrl = "jdbc:oracle:thin:@o2ordb.wuuxiang.com:1521:orclo2o";
	public static String readerUserName = "szslo2o";
	public static String readerPassword = "wxsl_16O2O_0525";

	public static String writerJdbcUrlGlobal = "jdbc:mysql://172.19.17.147:6306/i5x_global?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull";
	public static String writerUserNameGlobal = "i5x_global";
	public static String writerPasswordGlobal = "uGlobal-I5x0818c";

	public static String writerJdbcUrlPlatform = "jdbc:mysql://172.19.17.147:6306/i5x_platform?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull";
	public static String writerUserNamePlatform = "i5x_platform";
	public static String writerPasswordPlatform = "uPlaF0r-863xxd";

	public static String writerJdbcUrlBusiness = "jdbc:mysql://172.19.17.147:6306/i5x_business?useUnicode=true&characterEncoding=UTF-8&characterSetResults=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull";
	public static String writerUserNameBusiness = "i5x_business";
	public static String writerPasswordBusiness = "uBusein-7251I5x";


	public static void main(String[] args) throws Exception {
		String token = login();
		//global
		//createGlobalJob(token);
		//business
		createBusinessJob(token);
		//platform
		createPlatformJob(token);
	}

	public static void createGlobalJob(String token) throws Exception {
		String writerJdbcUrl = writerJdbcUrlGlobal;
		String writerUserName = writerUserNameGlobal;
		String writerPassword = writerPasswordGlobal;
		{
			//2
			String table = "wx_component";
			List<String> columnList = Lists.newArrayList(
					"component_appid",
					"component_verify_ticket",
					"component_access_token",
					"ticket_updata_date",
					"access_token_updata_date"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
	}

	public static void createBusinessJob(String token) throws Exception {
		String writerJdbcUrl = writerJdbcUrlBusiness;
		String writerUserName = writerUserNameBusiness;
		String writerPassword = writerPasswordBusiness;
		{
			//8
			String table = "gc_privacy";
			List<String> columnList = Lists.newArrayList(
					"id",
					"gc_id",
					"ver",
					"privacy_file_url",
					"update_time"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//557
			String table = "gc_privacy_user_relation";
			List<String> columnList = Lists.newArrayList(
					"id",
					"gc_id",
					"ver",
					"mpid",
					"openid",
					"update_time",
					"delete_flag"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1075
			String table = "user_favor_mc";
			List<String> columnList = Lists.newArrayList(
					"id",
					"gc_id",
					"mc_id",
					"mpid",
					"openid",
					"update_time",
					"delete_flag"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//8341616  ideleteflg = 0(1428814)   sysdate-2(23878)
			//select * from yu_dian_order where IDELETEFLG = 0 and DTCREATE > sysdate-2
			String table = "yu_dian_order";
			List<String> columnList = Lists.newArrayList(
					"id",
					"gcid",
					"mcid",
					"mc_name",
					"mpid",
					"openid",
					"unionid",
					"nick_name",
					"phoneno",
					"order_info",
					"order_time",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword, "ideleteflg=0", "dtcreate > sysdate-2");
		}
		{
			//49318096  sysdate-2(129398)
			//where IDELETEFLG = 0 and DTCREATE > sysdate-2
			String table = "yu_dian_order_item";
			List<String> columnList = Lists.newArrayList(
					"id",
					"gcid",
					"mcid",
					"openid",
					"order_id",
					"itemclassid",
					"itemid",
					"item_name",
					"item_price",
					"item_count",
					"item_detail",
					"tcflg",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword, "ideleteflg=0", "dtcreate > sysdate-2");
		}

	}

	public static void createPlatformJob(String token) throws Exception {
		String writerJdbcUrl = writerJdbcUrlPlatform;
		String writerUserName = writerUserNamePlatform;
		String writerPassword = writerPasswordPlatform;
		{
			//37082
			String table = "wxlogin";
			List<String> columnList = Lists.newArrayList(
					"iloginid",
					"cloginuser",
					"cloginname",
					"cpwd",
					"cmail",
					"cqq",
					"istatus",
					"dtcreate",
					"dtmodify"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//37371
			String table = "wxapiaddr";
			List<String> columnList = Lists.newArrayList(
					"iapiid",
					"iloginid",
					"capiname",
					"ctoken",
					"cwxusername",
					"cappid",
					"cappsecret",
					"cencodingaeskey",
					"iattendreplytype",
					"cattendreply",
					"iautoreplytype",
					"cautoreply",
					"isopenrefresh",
					"caccesstoken",
					"dttoken",
					"dtcreate",
					"dtmodify",
					"cjsapiticket",
					"dtjsapiticket",
					"ckqapiticket",
					"dtkqapiticket",
					"calipayappid",
					"authorize_refresh_token",
					"authorize_flg",
					"qrcode_url"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1166
			String table = "ali_mini_commit_temp";
			List<String> columnList = Lists.newArrayList(
					"id",
					"templateid",
					"extjson",
					"appversion",
					"auditid",
					"commitdate",
					"auditdate",
					"auditokdate",
					"status",
					"releasedate",
					"appid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//494
			String table = "ali_mini_rel_temp";
			List<String> columnList = Lists.newArrayList(
					"appid",
					"audittempid",
					"audiversion",
					"releasetempid",
					"releaseversion",
					"remark",
					"createdate"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//9
			String table = "ali_mini_temp";
			List<String> columnList = Lists.newArrayList(
					"templateid",
					"biztype",
					"version",
					"defaultflag",
					"appdesc"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//368079
			String table = "wx_mini_commit_temp";
			List<String> columnList = Lists.newArrayList(
					"id",
					"templateid",
					"extjson",
					"userversion",
					"userdesc",
					"auditid",
					"commitdate",
					"auditdate",
					"auditokdate",
					"status",
					"releasedate",
					"appid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//16727
			String table = "wx_mini_rel_temp";
			List<String> columnList = Lists.newArrayList(
					"id",
					"appid",
					"audittempid",
					"releasetempid",
					"remark",
					"createdate"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//12
			String table = "wx_mini_default_temp";
			List<String> columnList = Lists.newArrayList(
					"templateid",
					"userversion",
					"userdesc",
					"biztype",
					"grouptemplateid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//885
			String table = "alipay_miniprogram_template";
			List<String> columnList = Lists.newArrayList(
					"id",
					"templateid",
					"templateidshort",
					"mpid",
					"appid",
					"state",
					"remark",
					"dtcreate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//2
			String table = "alipay_mini_template_bizfun";
			List<String> columnList = Lists.newArrayList(
					"id",
					"bizfuncode",
					"mpid",
					"appid",
					"bizfundesc",
					"bizfunremarktext",
					"bizfunurl",
					"delaytime",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//82564
			String table = "wx_miniprogram_subscribe";
			List<String> columnList = Lists.newArrayList(
					"templateid",
					"templateidshort",
					"mpid",
					"appid",
					"dtcreate",
					"state",
					"remark",
					"contents",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//919
			String table = "wx_miniprogram_subscribe_biz";
			List<String> columnList = Lists.newArrayList(
					"id",
					"bizfuncode",
					"mpid",
					"appid",
					"bizfundesc",
					"bizfunremarktext",
					"bizfunurl",
					"delaytime",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1378
			String table = "wx_subscribe";
			List<String> columnList = Lists.newArrayList(
					"templateid",
					"templateidshort",
					"mpid",
					"state",
					"remark",
					"contents",
					"dtcreate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//47
			String table = "wx_subscribe_bizfun";
			List<String> columnList = Lists.newArrayList(
					"id",
					"bizfuncode",
					"mpid",
					"bizfundesc",
					"bizfunremarktext",
					"bizfunurl",
					"delaytime",
					"miniappid",
					"turntype",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//238560
			String table = "wx_template";
			List<String> columnList = Lists.newArrayList(
					"templateid",
					"templateidshort",
					"mpid",
					"appid",
					"state",
					"remark",
					"dtcreate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//23309
			String table = "wx_template_bizfun";
			List<String> columnList = Lists.newArrayList(
					"id",
					"bizfuncode",
					"mpid",
					"appid",
					"bizfundesc",
					"bizfunremarktext",
					"bizfunurl",
					"bizfunfirsttext",
					"bizfunfirstcolor",
					"bizfunremarkcolor",
					"delaytime",
					"miniappid",
					"minipagepath",
					"turntype",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1492
			String table = "wxmsgnews";
			List<String> columnList = Lists.newArrayList(
					"inewsid",
					"iapiid",
					"ireplytype",
					"cnewstitle",
					"cnewsdescription",
					"cnewspicurl",
					"cnewsurl"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1852
			String table = "wxmsgnewslibrary";
			List<String> columnList = Lists.newArrayList(
					"iapiid",
					"newslibrary_id",
					"create_date"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//21306
			String table = "wxcusmsg";
			List<String> columnList = Lists.newArrayList(
					"imsgid",
					"iapiid",
					"imsgitemid",
					"ckeyword",
					"imsgtype",
					"ireplymsgid",
					"iuse",
					"ifullmatch",
					"imenu",
					"dtcreate",
					"dtmodify"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//11229
			String table = "wxreplymsgtext";
			List<String> columnList = Lists.newArrayList(
					"ireplymsgid",
					"cmsgcontent"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//3777
			String table = "wxreplymsgnews";
			List<String> columnList = Lists.newArrayList(
					"inewsid",
					"ireplymsgid",
					"cnewstitle",
					"cnewsdescription",
					"cnewspicurl",
					"cnewsurl"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1080
			String table = "wxreplymsgnewslibrary";
			List<String> columnList = Lists.newArrayList(
					"ireplymsgid",
					"newslibrary_id",
					"create_date"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//29194
			String table = "wxnewslibrary";
			List<String> columnList = Lists.newArrayList(
					"id",
					"title",
					"content",
					"picurl",
					"url",
					"dtcreate",
					"dtupdate",
					"deleteflg",
					"dtdelete",
					"mediaid",
					"biztype",
					"picid",
					"gcid",
					"iapiid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//8692
			String table = "alipay_groupcorp";
			List<String> columnList = Lists.newArrayList(
					"alipay_app_id",
					"gcid",
					"createtime",
					"isrelation",
					"name",
					"type",
					"alipay_pid",
					"alipay_app_auth_token",
					"alipay_app_refresh_token"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//31788
			String table = "alipay_mc_relation";
			List<String> columnList = Lists.newArrayList(
					"id",
					"alipay_app_id",
					"mcid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//18147
			String table = "alipay_mc_relation_mini";
			List<String> columnList = Lists.newArrayList(
					"id",
					"alipay_app_id",
					"mcid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1586
			String table = "alipay_miniprogram";
			List<String> columnList = Lists.newArrayList(
					"id",
					"merchant_pid",
					"order_ticket",
					"contact_name",
					"contact_mobile",
					"contact_email",
					"batch_no",
					"app_name",
					"app_english_name",
					"app_category_id1",
					"app_category_id2",
					"app_slogan",
					"service_phone",
					"service_email",
					"app_logo",
					"app_desc",
					"business_license",
					"mc_license",
					"business_license_start_date",
					"business_license_end_date",
					"user_id",
					"auth_app_id",
					"appid",
					"app_auth_token",
					"app_refresh_token",
					"expires_in",
					"re_expires_in",
					"create_date",
					"update_date",
					"ideleteflg",
					"biztype",
					"deployflg"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//1568
			String table = "alipay_miniprogram_login";
			List<String> columnList = Lists.newArrayList(
					"imlid",
					"appid",
					"iloginid",
					"iapiid",
					"create_date",
					"update_date",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//46198
			String table = "wx_groupcorp";
			List<String> columnList = Lists.newArrayList(
					"wx_mp_id",
					"gcid",
					"isrelation",
					"name",
					"type",
					"createtime"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//78359
			String table = "wx_mc_relation";
			List<String> columnList = Lists.newArrayList(
					"id",
					"wx_mp_id",
					"mcid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//101199
			String table = "wx_mc_relation_mini";
			List<String> columnList = Lists.newArrayList(
					"id",
					"wx_mp_id",
					"mcid"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//17393
			String table = "wxminiprogram";
			List<String> columnList = Lists.newArrayList(
					"id",
					"cappid",
					"cmpid",
					"cnickname",
					"caccesstoken",
					"dtaccesstoken",
					"cauthrefreshtoken",
					"iauthflg",
					"topimg",
					"biztype",
					"remark",
					"subversion",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//29132
			String table = "wxwechat";
			List<String> columnList = Lists.newArrayList(
					"appid",
					"username",
					"principalname",
					"nikename",
					"alias",
					"qrcodeurl",
					"headimg",
					"dtcreate"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//12576
			String table = "pho_openplatform";
			List<String> columnList = Lists.newArrayList(
					"id",
					"gcid",
					"open_appid",
					"name",
					"create_date",
					"update_date",
					"ideleteflg",
					"delete_date"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//35170
			String table = "wx_gc_apiaddr";
			List<String> columnList = Lists.newArrayList(
					"id",
					"gcid",
					"iapiid",
					"cloginuser",
					"capiname",
					"wechat_file",
					"alipay_file",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//702
			String table = "alipay_servicemarket_order";
			List<String> columnList = Lists.newArrayList(
					"commodity_order_id",
					"app_id",
					"contactor",
					"merchant_pid",
					"name",
					"notify_id",
					"notify_time",
					"notify_type",
					"order_item_num",
					"order_ticket",
					"order_time",
					"phone",
					"timestamp",
					"title",
					"total_price",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
		{
			//18953
			String table = "wx_miniprogram_login";
			List<String> columnList = Lists.newArrayList(
					"imlid",
					"cappid",
					"iloginid",
					"dtcreate",
					"dtupdate",
					"ideleteflg",
					"dtdelete"
			);
			createJob(token, table, columnList, writerJdbcUrl, writerUserName, writerPassword);
		}
	}

	public static String login() throws Exception {
		String url = prefixUrl + "/login.do";
		JSONObject params = new JSONObject();
		params.put("username", userName);
		params.put("password", userPwd);
		JSONObject jsonResult = HttpUtil.doPostJSONObject(url, params.toJSONString());
		return jsonResult.getString("token");
	}

	public static void createJob(String token, String table, List<String> columnList, String writerJdbcUrl, String writerUserName, String writerPassword, String... whereSql) throws Exception {
		String url = prefixUrl + "/creatjob";
		Map<String, String> headerMap = new HashMap<>();
		headerMap.put("Authorization", token);
		JSONObject params = new JSONObject();
		params.put("id", "");
		params.put("jobName", table);
		params.put("jobDescription", table);
		params.put("jobtype", "1");
		params.put("jobgroup", jobGroup);
		params.put("shards", 5);
		params.put("workers", 1);
		params.put("httptype", "GET");
		params.put("url", "");
		params.put("source", "");
		params.put("source_auth", "");
		params.put("dest", "");
		params.put("dest_auth", "");
		params.put("query", "");
		params.put("src_indexes", "");
		params.put("dest_index", "");
		//jobConf
		JSONObject jobConf = new JSONObject();
		JSONObject job = new JSONObject();
		job.put("setting", JSONObject.parseObject("{\"speed\":{\"channel\":5}}"));
		//querySql
		String querySql = "select "
				+ StringUtils.join(columnList, ",")
				+ " from "
				+ table;
		if (whereSql != null && whereSql.length > 0) {
			for (int i = 0; i < whereSql.length; i++) {
				if (i == 0) {
					querySql = querySql + " where ";
				} else {
					querySql = querySql + " and ";
				}
				querySql = querySql + whereSql[i];
			}
		}
		JSONObject reader = new JSONObject();
		reader.fluentPut("name", "oraclereader")
				.fluentPut("parameter", new JSONObject()
						.fluentPut("username", readerUserName)
						.fluentPut("password", readerPassword)
						.fluentPut("where", "")
						.fluentPut("connection", new JSONArray()
								.fluentAdd(new JSONObject()
										.fluentPut("querySql", new JSONArray().fluentAdd(querySql))
										.fluentPut("jdbcUrl", new JSONArray().fluentAdd(readerJdbcUrl)))));
		JSONObject writer = new JSONObject();
		writer.fluentPut("name", "mysql8writer")
				.fluentPut("parameter", new JSONObject()
						.fluentPut("username", writerUserName)
						.fluentPut("password", writerPassword)
						.fluentPut("writeMode", "insert")
						.fluentPut("splitPk", "")
						.fluentPut("column", new JSONArray().fluentAddAll(columnList))
						.fluentPut("preSql", new JSONArray())
						.fluentPut("connection", new JSONArray()
								.fluentAdd(new JSONObject()
										.fluentPut("table", new JSONArray().fluentAdd(table))
										.fluentPut("jdbcUrl", writerJdbcUrl))));

		job.put("content", new JSONArray().fluentAdd(new JSONObject().fluentPut("reader", reader).fluentPut("writer", writer)));
		jobConf.put("job", job);
		params.put("jobConf", jobConf);

		HttpUtil.doPostJSONObject(url, params.toJSONString(), headerMap);
	}

}
